
from __future__ import division
import sys,os,time
from src.Equipments.LockIn import SR830
from src.Equipments.RFGenerator import E8257D
from src.Data.DataStructure import Data
from src.Equipments.multimeter import AgilentMulti
from numpy import *
#import pylab as plb
home_path=os.getcwd()+os.sep#Current path to this program
sys.path.append(home_path+"src"+os.sep)
data_path=home_path+"data"+os.sep#For saving later on data

test=True #If standalone without anything connected->True

#########################################################
### Parameters - Sweeping settings
#########################################################
frequency_start=7033-100 #MHz
frequency_stop=7033+100 #MHz
number_of_points=400
waiting_time=1	#Waiting time for each point
#########################################################
#########################################################


#########################################################
### Parameters - Lockin setup
#########################################################
#lockin=SR830("GPIB0::9::INSTR",test)
#lockin.Set_Measure_Rate(512) #Hz
#lockin.Set_LPF(12)#dB/oct
#lockin.Set_Sensitivity(0.00002) #V
#lockin.Set_Time_Constant(0.3)
#lockin.Set_Harm(3)
#lockin.Set_InterExter_Mode(0)
multi=AgilentMulti("GPIB0::13::INSTR",test)
#########################################################
#########################################################

#########################################################
### Parameters - RF setup
#########################################################
RF=E8257D("GPIB0::10::INSTR","FM",test)
RF.Set_Modulation(5232,100)	#Frequency Hz, Modulation deviation kHz / amplitude dB
RF.Set_Modulation_Output(False,False)	#Modulation On = True, LFOutput On = True
RF.Set_Power(3)	#dBm
RF.Set_Frequency(frequency_start)
RF.Set_Output_ONOFF(True)
#########################################################
#########################################################


#########################################################
### Parameters - Data saving
#########################################################
data=Data("data",data_path,number_of_points,2)#3 because we have 1 frequency and two channels from the lockin
#lockin.Reset_Memory()


#plotX=[]
#plotY1=[]
#plotY2=[]
#fig=plb.figure()
#ax1=fig.add_subplot(211)
#ax1.set_title("Realtime Waveform Plot")
#ax1.set_xlabel('Frequency (MHz)')
#ax1.set_ylabel('Signal intensity (V)')
#line1=ax1.plot(plb.array(data.data[:,0]),plb.array(data.data[:,1]),'o-')
#ax2=fig.add_subplot(212)
#ax2.set_title("Realtime Waveform Plot")
#ax2.set_xlabel('Frequency (MHz)')
#ax2.set_ylabel('Signal intensity (V)')
#line2=ax2.plot(plb.array(data.data[:,0]),plb.array(data.data[:,2]),'o-')
#manager = plb.get_current_fig_manager()

#line2,=ax2.plot(data.data[:,0],data.data[:,2],'o-')
#ax2.set_xlabel('Frequency (MHz)')
#ax2.set_ylabel('Signal intensity (V)')


#########################################################
#########################################################


#########################################################
### Measure
#########################################################
for i in range(0,number_of_points):
	#if i == 0:	#don't know why, just do it
	#	time.sleep(waiting_time)
	#	time.sleep(waiting_time)
		
	#lockin.Reset_Memory()
	#Change frequency
	frequency=frequency_start+i*(frequency_stop-frequency_start)/(number_of_points-1)
	RF.Set_Frequency(frequency)#MHz
	data.data[i,0]=frequency
	
	#Perform a measure
	#lockin.Start_Measure()
	time.sleep(waiting_time)#s, about 256 points
	#lockin.Stop_Measure()
	data.data[i,1]=multi.Measure()
	
	#read points
	#res1=lockin.Read_Points(1)
	#data.data[i,1]=sum(res1) / len(res1)
	#res2=lockin.Read_Points(2)
	#data.data[i,2]=sum(res2) / len(res2)
	#lockin.Reset_Memory()

	#print "f = ",frequency,"MHz, A = ",data.data[i,1],"V, B = ",data.data[i,2],"V"
	print "f = ",frequency,"MHz, A = ",data.data[i,1],"V"


	#plotX.append(frequency)
	#plotY1.append(data.data[i,1])
	#plotY2.append(data.data[i,2])

	#manager.show()
#########################################################
#########################################################

#line1[0].set_data(plotX,plotY1)
#line2[0].set_data(plotX,plotY2)
#ax1.axis([min(plotX),max(plotX),min(plotY1),max(plotY1)])
#ax2.axis([min(plotX),max(plotX),min(plotY2),max(plotY2)])
#manager.canvas.draw()
#plb.show()
data.Save("txt")
